package timing.ukulele.portal.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import timing.ukulele.data.portal.view.AntIconVO;
import timing.ukulele.portal.convertor.IconConverter;
import timing.ukulele.portal.mapper.AntIconMapper;
import timing.ukulele.portal.persistent.AntIcon;

import java.util.List;

@Service
public class AntIconService extends BaseOperatorService<AntIconMapper, AntIcon> {
    public IPage<AntIconVO> getPage(String type, String value, String theme, int current, int size) {
        Page<AntIcon> page = new Page<>(current, size);
        LambdaQueryWrapper<AntIcon> query = new LambdaQueryWrapper<>();
        query.eq(StringUtils.hasText(type), AntIcon::getType, type);
        query.like(StringUtils.hasText(value), AntIcon::getValue, value);
        query.eq(StringUtils.hasText(theme), AntIcon::getTheme, theme);
        query.orderByDesc(AntIcon::getId);
        IPage<AntIcon> iPage = this.getBaseMapper().selectPage(page, query);
        Page<AntIconVO> voPage = new Page<>(current, size);
        if (iPage != null && !CollectionUtils.isEmpty(iPage.getRecords())) {
            List<AntIconVO> voList = IconConverter.INSTANCE.p2vList(iPage.getRecords());
            voPage.setRecords(voList);
            voPage.setTotal(iPage.getTotal());
        }
        return voPage;
    }
}
